package com.cookie.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cookie.entity.Category;
import com.cookie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : cookie ✌️
 * @date : 2018/10/1 15:15
 */
@RestController
@RequestMapping("wx/api/category/")
@Slf4j
public class ApiCategoryController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("list")
    public String getCategories() {
        Object[] year = {"2018", "2017", "2016", "2015"};
        List<Category> categories = categoryService.selectList(new EntityWrapper<>());
        Object[] array = categories.stream().map(Category::getName).toArray();
        Object[][] names = {array, year};
        return JSON.toJSONString(names);
    }

    @GetMapping("map")
    public String getCategoryMap() {
        List<Category> categories = categoryService.selectList(new EntityWrapper<>());
        Map<String, Integer> collect = categories.stream().collect(Collectors.toMap(Category::getName, Category::getId));
        return JSON.toJSONString(collect);
    }
}
